﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace omaura_oled
{
    /// <summary>
    ///  Class to write the config file
    /// </summary>
    public class WriteConfigFile
    {
        #region "Attributes"
        //- 
        private OmauraOled OledInterface;

        //-- font
        public string FontType = "";
        public int FontTypeSize;

        //-- general
        public int OledIsEnable;
        public int OledBackGroundIsEnable;
        public string Language = "";
        public string Language2Char = "";

        //-- text/logo
        public int LogoIsEnable;
        public int TextSize;

        //-- theme
        public string PathToTheme = "";

        //-- weather
        public int WeatherTemperatureInCelcius;

        //-- messages
        public string StartMessage = "";
        public string EndMessage = "";

        //-- positions
        public int xIcon;
        public int yIcon;
        public int xText;
        public int yText;
        public int xClock;
        public int yClock;
        public int xTimer;
        public int yTimer;

        //-- plugin 
        public int PluginIsEnable;

        //- path to the file
        private string PathToConfig_ini = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\omaura\\omaura oled\\Config.ini";
        //--
        #endregion "Attributes"

        #region "Constructor"
        //--
        public WriteConfigFile(OmauraOled parentOLED)
        {
            OledInterface = parentOLED;
        }
        //--
        #endregion "Constructor"

        #region "Methods"
        // WRITE

        // 1. Write the interface parameters into the current config file
        public void WriteCurrentTools()
        {
            // Create a new file 
            // - copy the content of config.ini in this new file
            // - modify the parameters required
            // - erase the config.ini file by the modified one
            int i = 0;
            string line;
            string firstPara;
            string lineTemp = "";

            #region "Create temp file"
            //--
            using (StreamReader fileReader = new StreamReader(PathToConfig_ini, Encoding.Default))
            {
                // read line by line 
                // - if line contain parameters to modify: modify line and copy it
                // - if not just copy
                while (((line = fileReader.ReadLine()) != null) && i < 255)
                {
                    i++;

                    firstPara = line[0].ToString();
                    for (int charI = 1; charI < 4; charI++)
                        firstPara += line[charI].ToString();

                    #region "Font"
                    //-- no interface modification possible

                    //--
                    #endregion "Font"

                    #region "General"
                    //--
                    if (firstPara == "OIE ")
                        lineTemp = "OIE  = " + OledIsEnable;
                    else if (firstPara == "OBIE")
                        lineTemp = "OBIE = " + OledBackGroundIsEnable;
                    else if (firstPara == "LANG")
                        lineTemp = "LANG = |" + Language2Char + "|";
                    //--
                    #endregion "General"

                    #region "Text/Logo"
                    //--
                    else if (firstPara == "LOIE")
                        lineTemp = "LOIE = " + LogoIsEnable;
                    else if (firstPara == "TS  ")
                        lineTemp = "TS   = " + TextSize;
                    //--
                    #endregion "Text/Logo"

                    #region "Theme"
                    //--
                    else if (firstPara == "PaTT")
                        lineTemp = "PaTT = |" + PathToTheme + "|";
                    //--
                    #endregion "Theme"

                    #region "Weather"
                    //--
                    else if (firstPara == "WTIC")
                        lineTemp = "WTIC = " + WeatherTemperatureInCelcius;
                    //--
                    #endregion "Weather"

                    #region "Messages"
                    //-- no interface modification possible

                    //--
                    #endregion "Messages"

                    #region "Positions"
                    //-- no interface modification possible

                    //--
                    #endregion "Positions"

                    #region "Plugin"
                        //--
                    else if (firstPara == "PisE")
                        lineTemp = "PisE = " + PluginIsEnable;
                    //--
                    #endregion "Plugin"

                    // else line
                    else
                    {
                        lineTemp = line;
                    }

                    // Copy the lineTemp
                    using (StreamWriter fileWriter = new StreamWriter(PathToConfig_ini + ".tmp", true, Encoding.Default))
                        fileWriter.WriteLine(lineTemp);
                }
            }

            //--
            #endregion "Create temp file"

            #region "Copy temp File"
            //-- Delete old config file
            File.Delete(PathToConfig_ini);
            //-- Recreate the config file from the temp one
            File.Copy(PathToConfig_ini + ".tmp", PathToConfig_ini);
            //--
            #endregion "Copy temp File"

            #region "Delete temp File"
            //-- Delete Temp File
            File.Delete(PathToConfig_ini + ".tmp");
            //--
            #endregion "Delete temp File"
        }

        // 2. Copy the entire file
        //      - themes tab
        public void CopyToCurrentTheme()
        {
            // Delete old config file
            File.Delete(PathToConfig_ini);
            // copy the config file from the iconPath one
            File.Copy(".\\Resources\\Themes\\" + PathToTheme + "\\Config.ini", PathToConfig_ini);
        }

        // 3. Restore default
        //      - restore the default config.ini file
        public void RestoreDefaultTheme()
        {
            // Delete old config file
            File.Delete(PathToConfig_ini);
            // Recreate the config file from the temp one
            File.Copy(".\\Resources\\Themes\\Omaura(Default)\\Config.ini", PathToConfig_ini);
        }


        // READ

        // 1. Method to read the informations in the interface
        public void ReadInterface()
        {
            #region "General"
            //-- OledIsEnable
            if (OledInterface.radioOledActiveOn.IsChecked.Value)
                OledIsEnable = 1;
            else
                OledIsEnable = 0;
            //-- BackGroundIsEnable
            if (OledInterface.radioBgOn.IsChecked.Value)
                OledBackGroundIsEnable = 1;
            else
                OledBackGroundIsEnable = 0;
            //-- Language
            string languageValue = OledInterface.LanguageSearch.Get2LettersForLanguage(OledInterface.comboBoxLanguage.Text, OledInterface);//SelectedItem.ToString());
            if (!languageValue.Equals("error"))
            {
                Language = OledInterface.InstallPath + ".\\Resources\\Lang\\" + languageValue + ".txt";
                Language2Char = languageValue;
            }
            else
            {
                Language = OledInterface.InstallPath + ".\\Resources\\Lang\\en.txt";
                Language2Char = "en";
            }
            //--
            #endregion "General"

            #region "Messages"
            //-- Logo
            if (OledInterface.radioButtonToolsLogo.IsChecked.Value)
                LogoIsEnable = 1;
            else
                LogoIsEnable = 0;
            //-- Text Size
            if (OledInterface.radioButtonToolsMessagesTextSmall.IsChecked.Value)
                TextSize = 0;
            else if (OledInterface.radioButtonToolsMessagesTextBig.IsChecked.Value)
                TextSize = 2;
            else
                TextSize = 1;
            //--
            #endregion "Messages"

            #region "Themes"
            //--
            PathToTheme = OledInterface.listThemes.SelectedItem.ToString();
            //--
            #endregion "Themes"
            
            #region "Weather"
            //--
            if (OledInterface.radioTemperatureCelcius.IsChecked.Value)
                WeatherTemperatureInCelcius = 1;
            else
                WeatherTemperatureInCelcius = 0;
            //--
            #endregion "Weather"

            #region "Plugin"
            //--
            if (OledInterface.radioPluginEnable.IsChecked.Value)
                PluginIsEnable = 1;
            else
                PluginIsEnable = 0;
            //--
            #endregion "Plugin"
        }

        // 2. Method to read in a specific file and get the data refering to the parameter
        // return a string by giving 2strings parameters
        public string ReadInFileWithParameter(string FileName, string Parameter)
        {
            int i = 0;
            string firstPara;
            string line;
            string parameterData= "";

            using (StreamReader fileReader = new StreamReader(FileName, Encoding.Default, true))
            {
                while (((line = fileReader.ReadLine()) != null) && i < 255)
                {
                    i++;

                    firstPara = line[0].ToString();
                    for (int charI = 1; charI < 4; charI++)
                        firstPara += line[charI].ToString();

                    if (firstPara.Equals(Parameter))
                    {
                        // Read the data
                        if (line[7] == '|')
                        {
                            int iSM = 8;

                            while (line[iSM] != '|')
                            {
                                parameterData += line[iSM];
                                iSM++;
                            }
                        }
                        else if (line.Length > 9 && line[7] != '|')
                        {
                            parameterData = line[7].ToString() + line[8].ToString() + line[9].ToString();
                        }
                        else if (line.Length == 9 && line[7] != '|')
                        {
                            parameterData = line[7].ToString() + line[8].ToString();
                        }
                        else
                        {
                            parameterData = line[7].ToString();
                        }
                    }
                }
            }
            // Return the data
            return (parameterData);
        }
        //--
        #endregion "Methods"
    }
}